
package com.eclink.iot.rfidclient.manager;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author xyang
 */


public class CommandContent {
    
    public final byte pHead = (byte) 0xA0;
    public byte pLength = 0;
    public byte pAddress = 0;
    public byte pCommand = 0;
    public byte [] pData;
    public byte pCheckSum = 0;
    
    public Object [] GetCommandBytes(){
        
        List<Byte> bufflist =new ArrayList<>();
        pLength = (byte)(1 + 1+ pData.length + 1);
        
        bufflist.add(pHead);
        bufflist.add(pLength);
        bufflist.add(pAddress);
        bufflist.add(pCommand);
        
        for (int i = 0; i < pData.length; i++) {
            bufflist.add(pData[i]);
        }
        
        pCheckSum = getCheckSum(bufflist);
        bufflist.add(pCheckSum);
        
        return bufflist.toArray();
    }
    
    //获取crc
    public static byte getCheckSum(List<Byte> buff){
        byte checksum = 0x00;

        for (int i = 0; i < buff.size(); i++) {
            checksum += buff.get(i);
        }

        checksum = (byte)~checksum;
        checksum += 1;

        return checksum;
    }
}
